package com.zzh.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.zzh.db.dao.UserDao
import com.zzh.db.data.User

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-08-19
 * @desc
 */
@Database(version = 1, entities = [User::class])
abstract class AppDatabase : RoomDatabase() {


    abstract fun getUserDao(): UserDao

    companion object {

        lateinit var appDataBase: AppDatabase

        fun newInstance(context: Context): AppDatabase {
            return if (this::appDataBase.isInitialized) appDataBase else Room.databaseBuilder(
                context,
                AppDatabase::class.java,
                "testRoom"
            ).allowMainThreadQueries()
                .build().apply { appDataBase = this }
        }


    }
}